// import axios from "axios"
// import querystring from "querystring"

// const errorHandle = (status,info) => {
//   switch(status) {
//     case 400:
//       console.log("语义有误");
//       break;
//     case 401:
//       console.log("服务器认证失败")
//       break;
//     case 403:
//       console.log("服务器拒绝访问");
//       break;
//     case 404:
//       console.log("地址错误");
//       break;
//     case 500:
//       console.log("服务器遇到意外");
//       break;
//     case 502:
//       console.log("服务器无响应");
//       break;
//     default:
//       console.log(info);
//       break;
//   }
// }

// const instance =axios.create({
//   //网络请求的公共配置
//   timeout:5000
// })

// //最常用的拦截器

// //发送数据之前
// instance.interceptors.request.use(
//   config =>{
//     if(config.methods === "post") {
//       config.data=querystring.stringify(config.data);
//       //config:包含着网络请求的所有信息
//       return config;
//     }
//   },
//   error =>{
//     return Promise.reject(error)
//   }
// )

// //获取数据之前
// instance.interceptors.response.use(
//   response =>{
//     console.log("请求体：",response);
//     return Promise.resolve(response) 
//     // return response.status === 200 ?Promise.resolve(response) : Promise.reject(response)
//   },
//   error =>{
//     // const { response } = error;
//     // console.log("错误信息",response);
//     // errorHandle(response.status,response.info)
//     console.log("错误信息",error);
//     return Promise.reject(error)
//   }
// )

// export default instance;




// 1. 导入axios和Vue
import axios from 'axios';
import { reactive } from 'vue';

// 2. 创建一个axios实例
const http = axios.create({
  baseURL: '', // 根据你的实际情况修改
});

// 3. 创建请求拦截器
http.interceptors.request.use((config) => {
  // 可以在这里做一些请求前的处理，如添加token等
  return config;
}, (error) => {
  return Promise.reject(error);
});

// 4. 创建响应拦截器
http.interceptors.response.use((response) => {
  // 可以在这里做一些响应后的处理，如错误处理、数据统一处理等
  return response.data;
}, (error) => {
  return Promise.reject(error);
});

// 5. 封装GET请求方法
function get(url,params) {

  if(localStorage.getItem("access_token")==null) {
    localStorage.setItem("access_token","");
    }
  return http.get(url, { 
    params:params,
    headers: {
      Authorization: "Bearer "+localStorage.getItem("access_token")
    }
  });
}


function getWithParam(url, params) {
  if (localStorage.getItem("access_token") == null) {
    localStorage.setItem("access_token", "");
  }
  
  let endpoint = url;
  
  // Check if params exist
  const queryString = new URLSearchParams(params).toString();
  const requestUrl = `${endpoint}?${queryString}`;
  console.log("http://localhost:9876"+requestUrl);
  
  return http.get(requestUrl,{}, {
     headers: {
     'Accept': 'application/json', // 指示服务器返回JSON格式数据
     Authorization: "Bearer " + localStorage.getItem("access_token")
     }
    }); 
}


function put(url, params) {
  if (localStorage.getItem("access_token") == null) {
    localStorage.setItem("access_token", "");
  }
  
  let endpoint = url;
  
  // Check if params exist
  const queryString = new URLSearchParams(params).toString();
  const requestUrl = `${endpoint}?${queryString}`;
  console.log("http://localhost:9876"+requestUrl);
  
  return http.put(requestUrl, {
     headers: {
     'Accept': 'application/json', // 指示服务器返回JSON格式数据
     Authorization: "Bearer " + localStorage.getItem("access_token")
     }
    }); 
}


function deleteMethod(url, params) {
  if (localStorage.getItem("access_token") == null) {
    localStorage.setItem("access_token", "");
  }
  
  let endpoint = url;
  
  // Check if params exist
  const queryString = new URLSearchParams(params).toString();
  const requestUrl = `${endpoint}?${queryString}`;
  console.log("http://localhost:9876"+requestUrl);
  
  return http.delete(requestUrl, {
     headers: {
     'Accept': 'application/json', // 指示服务器返回JSON格式数据
     Authorization: "Bearer " + localStorage.getItem("access_token")
     }
    }); 
}

// 6. 封装POST请求方法
function post(url, data) {
 
  if(localStorage.getItem("access_token")==null) {
    localStorage.setItem("access_token","");
    }

  return http.post(url, data,{
    headers: {
      Authorization: "Bearer "+localStorage.getItem("access_token")
    }
  });
}

// // 6. 封装POST请求方法
// function post2(url, data) {
//   return http.post(url, data,{
//     headers: {
//     'Content-Type': 'multipart/form-data',
//       Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyMSIsImV4cCI6MTY5Mjg1MTI5OX0.e-6w7AD2vfE4IXLJweS-k8FMtlEvurB_VpqJL0qbJDg"
//     }
//   });
// }
//上传文件
function upload(url, data) {
  if(localStorage.getItem("access_token")==null) {
    localStorage.setItem("access_token","0");
    }
  return http.post(url, data,{
    headers: {
      'Content-Type': 'multipart/form-data',
       Authorization: "Bearer "+localStorage.getItem("access_token")
    }
  });
}


// 7. 导出网络请求相关方法
export default {
  get,
  getWithParam,
  put,
  deleteMethod,
  post,
  upload
};